﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Threading;
using System.Globalization;

namespace MockSender
{
    class Program
    {
        static void Main(string[] args)
        {
            // ustawic adres
            string baseUrl = "http://localhost:1542/Locations.asmx";

            Console.Out.Write("Wpisz nicka: ");
            string nickname = Console.In.ReadLine();

            Random randonGen = new Random();
            string color = "rgb(" + randonGen.Next(255) + "," + randonGen.Next(255) + "," + randonGen.Next(255) + ")";

            string data = "nickname=" + nickname + "&color=" + color;
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            HttpWebRequest registerReq = (HttpWebRequest)WebRequest.Create(baseUrl + "/Register");
            registerReq.ContentType = "application/x-www-form-urlencoded";
            registerReq.Method = "POST";
            registerReq.ContentLength = bytes.Length;
            registerReq.GetRequestStream().Write(bytes, 0, bytes.Length);
            registerReq.GetRequestStream().Close();

            string xmlResponse;
            try
            {
                HttpWebResponse registerRes = (HttpWebResponse)registerReq.GetResponse();
                using (StreamReader sr = new StreamReader(registerRes.GetResponseStream()))
                {
                    xmlResponse = sr.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                using (StreamReader sr = new StreamReader(e.Response.GetResponseStream()))
                {
                    Console.Out.WriteLine(sr.ReadToEnd());
                }
                Console.In.ReadLine();
                return;
            }

            XmlDocument doc = new XmlDocument();
            doc.InnerXml = xmlResponse;
            string sid = doc.LastChild.LastChild.Value;

            double lat = (randonGen.NextDouble() * 0.02) - 0.01 + 50.06;
            double lon = (randonGen.NextDouble() * 0.02) - 0.01 + 19.91;
            while (true)
            {
                try
                {
                    string locationString = "sid=" + sid + "&lat=" + lat.ToString(CultureInfo.InvariantCulture) + "&lon=" + lon.ToString(CultureInfo.InvariantCulture);
                    byte[] locationBytes = Encoding.UTF8.GetBytes(locationString);
                    HttpWebRequest addLocationReq = (HttpWebRequest)WebRequest.Create(baseUrl + "/AddLocation");
                    addLocationReq.ContentType = "application/x-www-form-urlencoded";
                    addLocationReq.Method = "POST";
                    addLocationReq.ContentLength = locationBytes.Length;
                    addLocationReq.GetRequestStream().Write(locationBytes, 0, locationBytes.Length);
                    addLocationReq.GetRequestStream().Close();
                    addLocationReq.GetResponse().Close();
                    lat += (randonGen.NextDouble() * 0.01) - 0.005;
                    lon += (randonGen.NextDouble() * 0.01) - 0.005;
                    Thread.Sleep(1500);
                }
                catch (WebException e)
                {
                    using (StreamReader sr = new StreamReader(e.Response.GetResponseStream()))
                    {
                        Console.Out.WriteLine(sr.ReadToEnd());
                    }
                    Console.In.ReadLine();
                    return;
                }
            }
        }
    }
}
